// pages/components/popup.js
var interval = null //倒计时函数
Component({

  properties: {


    dialogHidden: {

      type: Boolean,

      value: true

    },


    // 这里定义了innerText属性，属性值可以在组件使用时指定

    titleText: {

      type: String,

      value: '提示',

    },

    titleMsg: {

      type: String,

      value: ' ',

    },

    determineBtn: {

      type: String,

      value: 'default value',

    },

    //取消

    cancleBtn: {

      type: Boolean,

      value: true,

    },


  },



  data: {

    // 这里是一些组件内部数据
    inputValue: "",
    inputValue_1: "",
    inputValue_2: "",
    inputValue_3: "",
    array: ['学生', '家长', '其他'],
    index: '',
    region: '',
    time: '获取验证码', //倒计时 
    currentTime: 61,
    focus: false
  },


  methods: {

    // 获取验证码
    beginTime: function (options) {
      var that = this;
      var currentTime = that.data.currentTime
      let interval = setInterval(function () {
        currentTime--;
        that.setData({
          time: currentTime + '秒'
        })
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            time: '重新发送',
            currentTime: 61,
            disabled: false
          })
        }
      }, 1000)  
    },

    getCode: function () {
      var that = this;
      var session = wx.getStorageSync('session');
      console.log(session)
      wx.request({
        url: 'https://jiaoyu.mogochina.com/public/index.php/api/verification',
        method: 'post',
        data: {
          phone: that.data.inputValue_1,
          openid: session
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data;
          if (data.status == 200) {
            wx.showToast({
              title: data.msg,
              duration: 2000,
              success: function(){
                that.beginTime();
                that.setData({
                  disabled: true
                })
              }
            })
          } else {
            wx.showToast({
              title: data.error,
              duration: 2000
            })
          }
        },
        fail: function (e) {
          wx.showToast({
            title: '网络异常！err:getsessionkeys',
            duration: 2000
          });
        },
      })
       
        // var phoneNum = {phone: that.data.inputValue_1};
        // that.triggerEvent('getCode', phoneNum);
    },

    
    bindPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        index: e.detail.value
      })
    },

    bindRegionChange: function (e) {
      this.setData({
        region: e.detail.value
      })
    },
    // 输入值

    bindKeyInput: function (e) {

      this.setData({

        inputValue: e.detail.value

      })

    },
    bindKeyInput1: function (e) {

      this.setData({

        inputValue_1: e.detail.value

      })

    },
    bindKeyInput2: function (e) {

      this.setData({

        inputValue_2: e.detail.value

      })

    },
    bindKeyInput3: function (e) {

      this.setData({

        inputValue_3: e.detail.value

      })

    },

    // 这里是一个自定义方法,取消

    cancleBtn: function () {
      var isHidden = true;
      this.triggerEvent('cancleevent', isHidden)
      this.setData({
        dialogHidden: true,
        focus: false
      })
      this.triggerEvent('determineevent')
    },


    // 确定

    determineBtn: function () {
      var that = this;
      var session = wx.getStorageSync('session');
      var role = this.data.index;
      if(!role){
        role = ''
      }else{
        role = parseInt(this.data.index) + 1;
      }
      if (!that.data.inputValue || !that.data.inputValue_1 || !that.data.inputValue_2){
        wx.showToast({
          title: '请您完善信息后再提交！',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
      var determineDetail = { name: that.data.inputValue, phone: that.data.inputValue_1, code: that.data.inputValue_2, role: role, city: that.data.inputValue_3};
      determineDetail.openid = session;
      console.log(determineDetail);
      wx.request({
        url: 'https://jiaoyu.mogochina.com/public/index.php/api/person',
        method: 'post',
        data: determineDetail,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data;
          console.log(data);
          if (data.data == 1) {
            wx.showModal({
              title: '注册成功！',
              content: data.extra,
              success: function (res) {
                if (res.confirm) {
                  that.setData({
                    dialogHidden: true
                  })
                  // 刷新页面
                  that.triggerEvent('determineevent')
                } else if (res.cancel) {
                  that.setData({
                    dialogHidden: true
                  })
                  // 刷新页面
                  that.triggerEvent('determineevent')
                }
              }
            });
          } else {
            wx.showToast({
              title: data.error,
              icon: 'none',
              duration: 2000
            });
          }
        },
        fail: function (e) {
          wx.showToast({
            title: '网络异常！err:getsessionkeys',
            duration: 2000
          });
        },
      })
      // this.triggerEvent('determineevent', determineDetail)


      this.setData({
        // inputValue: "",
        // inputValue_1: ""
      })

    }

  }

})